#include "Exchange.h"

double Exchange::exchangeRateEurToUsd = 1;

Exchange::Exchange(void)
{
}

Exchange::Exchange(double eur, double usd)
{
	this->eur = eur;
	this->usd = usd;
}

Exchange::~Exchange(void)
{
}

void Exchange::setEURToUSDExchangeRate(double rate)
{
	exchangeRateEurToUsd = rate;
}

double Exchange::convert(double amount, bool eurToUsd)
{
	if(eurToUsd)
		return amount * exchangeRateEurToUsd;
	else return amount / exchangeRateEurToUsd;
}

double Exchange::exchange(double amount, bool eurToUsd)
{
	if(eurToUsd)
	{
		this->eur += amount;
		double usdReturn = this->convert(amount, true);
		if(this->usd >= usdReturn)
		{
			this->usd -= usdReturn;
			return usdReturn;
		} else return -1;
		
	}
	else {
		this->usd += amount;
		double eurReturn = this->convert(amount, false);
		if(this->eur >= eurReturn)
		{
			this->eur -= eurReturn;
			return eurReturn;
		} else return -1;
		
	}
}
